package com.honey.compilation.sql;

import java.util.ArrayList;
import java.util.List;

import com.honey.compilation.sql.ddl.SQL92Comment;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.Compilation;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.IndentSpace;
import com.honey.core.types.FullyQualifiedJavaType;

/**
 * sql 文件编译器
 * @author Administrator
 *
 */
public class SqlCompilation implements  Compilation{
	
	/** 文件头注释 */
	private SqlComment fileComment;
	
	
	/** 文件尾部注释 */
	private SqlComment afterBodyComment;
	
	
	/** sql脚本 */
	private List<SqlScript> scripts;
	
	/** 包路径和名称 */
	private FullyQualifiedJavaType type= null;
	
	/**
	 * 构造函数
	 */
	public SqlCompilation(FullyQualifiedJavaType type){
		fileComment = new SQL92Comment();
		afterBodyComment = new SQL92Comment();
		scripts = new ArrayList<SqlScript>();
		this.type = type;
		
	}
	
	/**
	 * 构造函数
	 * @param scripts sql脚本
	 */
	public SqlCompilation(FullyQualifiedJavaType type, SqlScript... scripts){
		this(type);
		addSqlScript(scripts);
	}
	
	/**
	 * 添加sql脚本
	 * @param scripts
	 */
	public void addSqlScript(SqlScript... scripts) {
		for(SqlScript script : scripts ){
			if(script!= null){
				this.scripts.add(script);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.core.compiler.Compilation#addFileComment(java.lang.String[])
	 */
	@Override
	public void addFileComment(String... comments) throws CompilationException {
		this.fileComment.addComments(comments);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.compiler.Compilation#addAfterBodyComment(java.lang.String[])
	 */
	@Override
	public void addAfterBodyComment(String... comments) throws CompilationException {
		this.afterBodyComment.addComments(comments);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.dom.Compilation#getFileComments()
	 */
	@Override
	public List<String> getFileComments() throws CompilationException {
		return this.fileComment.getComments();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.dom.Compilation#compiledContent()
	 */
	@Override
	public CharacterBuilder compiledContent() throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder();
		int  indentLevel = 0 ;
		answer.append(fileComment.compiledContent(indentLevel));
		for(SqlScript script : scripts){
			answer.append(script.compiledContent(0));
		}
		IndentSpace.newLine(answer);
		IndentSpace.newLine(answer);
		answer.append(afterBodyComment.compiledContent(indentLevel));
		return answer;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.core.compiler.Compilation#getType()
	 */
	@Override
	public FullyQualifiedJavaType getType() {
		return type;
	}

}
